
package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.mapper.AttendanceRecordMapper;
import com.ruoyi.system.domain.AttendanceRecord;
import com.ruoyi.system.service.IAttendanceRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.Calendar;

/**
 * 考勤记录Service业务层处理
 */
@Service
public class AttendanceRecordServiceImpl implements IAttendanceRecordService {

    @Autowired
    private AttendanceRecordMapper attendanceRecordMapper;

    // 上班打卡标准时间（9:00）
    private static final int CHECK_IN_STANDARD_HOUR = 9;
    // 下班打卡标准时间（18:00）
    private static final int CHECK_OUT_STANDARD_HOUR = 18;

    @Override
    public int handleCheckIn() {
        // 1. 获取当前用户ID
        Long userId = SecurityUtils.getUserId();

        // 2. 检查今天是否已打卡
        AttendanceRecord todayRecord = attendanceRecordMapper.selectTodayRecordByUserId(userId);
        if (todayRecord != null && todayRecord.getCheckInTime() != null) {
            throw new ServiceException("今日已打卡上班，请勿重复操作");
        }

        // 3. 创建打卡记录（新记录或更新记录）
        AttendanceRecord record = todayRecord != null ? todayRecord : new AttendanceRecord();
        Date now = new Date();

        // 4. 设置打卡信息
        record.setUserId(userId);
        record.setCheckInTime(now);
        record.setCheckInMethod("MANUAL");
        record.setWorkDate(now);

        // 5. 判断是否迟到
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.set(Calendar.HOUR_OF_DAY, CHECK_IN_STANDARD_HOUR);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);

        if (now.after(cal.getTime())) {
            record.setStatus("LATE"); // 迟到
        } else {
            record.setStatus("NORMAL"); // 正常
        }

        // 6. 保存记录
        if (todayRecord == null) {
            return attendanceRecordMapper.insertAttendanceRecord(record);
        } else {
            return attendanceRecordMapper.updateAttendanceRecord(record);
        }
    }

    @Override
    public int handleCheckOut() {
        // 1. 获取当前用户ID
        Long userId = SecurityUtils.getUserId();

        // 2. 查询今日打卡记录
        AttendanceRecord todayRecord = attendanceRecordMapper.selectTodayRecordByUserId(userId);
        if (todayRecord == null) {
            throw new ServiceException("请先完成上班打卡");
        }

        // 3. 检查是否已下班打卡
        if (todayRecord.getCheckOutTime() != null) {
            throw new ServiceException("今日已打卡下班，请勿重复操作");
        }

        // 4. 设置下班打卡信息
        Date now = new Date();
        todayRecord.setCheckOutTime(now);
        todayRecord.setCheckOutMethod("MANUAL");

        // 5. 检查是否早退
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.set(Calendar.HOUR_OF_DAY, CHECK_OUT_STANDARD_HOUR);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
//------------------------------------------------------------------
        if (now.before(cal.getTime())) {
            if (!"LATE".equals(todayRecord.getStatus())) {
                todayRecord.setStatus("EARLY_LEAVE"); // 早退
            }
            // 如果既迟到又早退，保持迟到状态
        }

        // 6. 更新记录
        return attendanceRecordMapper.updateAttendanceRecord(todayRecord);
    }

    @Override
    public AttendanceRecord selectTodayRecordByUserId(Long userId) {
        return attendanceRecordMapper.selectTodayRecordByUserId(userId);
    }

    // 以下是原有方法的实现保持不变...
    @Override
    public AttendanceRecord selectAttendanceRecordById(Long id) {
        return attendanceRecordMapper.selectAttendanceRecordById(id);
    }

    @Override
    public List<AttendanceRecord> selectAttendanceRecordList(AttendanceRecord attendanceRecord) {
        return attendanceRecordMapper.selectAttendanceRecordList(attendanceRecord);
    }

    @Override
    public int insertAttendanceRecord(AttendanceRecord attendanceRecord) {
        return attendanceRecordMapper.insertAttendanceRecord(attendanceRecord);
    }

    @Override
    public int updateAttendanceRecord(AttendanceRecord attendanceRecord) {
        return attendanceRecordMapper.updateAttendanceRecord(attendanceRecord);
    }

    @Override
    public int deleteAttendanceRecordByIds(Long[] ids) {
        return attendanceRecordMapper.deleteAttendanceRecordByIds(ids);
    }

    @Override
    public int deleteAttendanceRecordById(Long id) {
        return attendanceRecordMapper.deleteAttendanceRecordById(id);
    }

}